package tcu.iot.gasmanage.service;

import org.springframework.stereotype.Service;
import tcu.iot.gasmanage.common.Result;
import tcu.iot.gasmanage.common.utils.DateTimeUtils;
import tcu.iot.gasmanage.common.utils.DateUtils;
import tcu.iot.gasmanage.entity.alarm.AlarmEventsQuery;
import tcu.iot.gasmanage.entity.alarm.AlarmEventsReturn;
import tcu.iot.gasmanage.entity.equipment.Equipment;
import tcu.iot.gasmanage.entity.equipment.EquipmentInfoResult;
import tcu.iot.gasmanage.mapper.AlarmMapper;
import tcu.iot.gasmanage.mapper.DataTableMapper;
import tcu.iot.gasmanage.mapper.TDengineMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class AlarmService {

    @Resource
    private AlarmMapper alarmMapper;
    @Resource
    private DataTableMapper dataTableMapper;

    public Result getAlarmData(AlarmEventsQuery alarmEventsQuery) {

        List<AlarmEventsReturn> returnList = new ArrayList<>();
        HashMap<String, Object> hashMap = new HashMap();

        String startDate;
        String endDate;

        Float i1Limit = alarmEventsQuery.getDataLimit().get(0);
        Float i2Limit = alarmEventsQuery.getDataLimit().get(1);
        Float i3Limit = alarmEventsQuery.getDataLimit().get(2);
        Float i4Limit = alarmEventsQuery.getDataLimit().get(3);

        if (alarmEventsQuery.getDate() == null) {
            startDate = DateUtils.SetTimeReduce1(DateTimeUtils.getDateTime()) + " " + DateTimeUtils.getTime();
            endDate = DateTimeUtils.getDateTime();

        } else {
            startDate = DateUtils.dealDateFormat(alarmEventsQuery.getDate().get(0));
            endDate = DateUtils.dealDateFormat(alarmEventsQuery.getDate().get(1));

        }


        if (i1Limit == null || i2Limit == null || i3Limit == null || i4Limit == null){
            i1Limit= (float) -2000;
            i2Limit= (float) -2000;
            i3Limit= (float) -2000;
            i4Limit= (float) -2000;
        }


        if (i1Limit >= 0 || i2Limit >= 0 || i3Limit >= 0 || i4Limit >= 0)
            return Result.failed("false", 400, "阈值取值不能大于0，请重新输入");

        List<EquipmentInfoResult> equipmentInfoResultList = TDengineMapper.DateEquipmentInfoData(alarmEventsQuery.getEquipmentNumber(), startDate, endDate);

        Equipment equipment = dataTableMapper.FindAEquipment(alarmEventsQuery.getEquipmentNumber());

        for (EquipmentInfoResult equipmentInfoResult : equipmentInfoResultList) {
            String i1Type = "(正常)";
            String i2Type = "(正常)";
            String i3Type = "(正常)";
            String i4Type = "(正常)";

            if (equipmentInfoResult.getI1() < i1Limit) {
                i1Type = "(异常)";
            }
            if (equipmentInfoResult.getI2() < i2Limit) {
                i2Type = "(异常)";
            }
            if (equipmentInfoResult.getI3() < i3Limit) {
                i3Type = "(异常)";
            }
            if (equipmentInfoResult.getI4() < i4Limit) {
                i4Type = "(异常)";
            }

            if (i1Type.equals("(异常)") || i2Type.equals("(异常)") || i3Type.equals("(异常)") || i4Type.equals("(异常)")) {
                AlarmEventsReturn alarmEventsReturn = new AlarmEventsReturn();

                alarmEventsReturn.setAlarmTime(equipmentInfoResult.getTime());
                alarmEventsReturn.setEquipmentNumber(alarmEventsQuery.getEquipmentNumber());
                alarmEventsReturn.setPlace(equipment.getRegion());
                alarmEventsReturn.setSite(equipment.getSite());
                alarmEventsReturn.setI1(equipmentInfoResult.getI1() + i1Type);
                alarmEventsReturn.setI2(equipmentInfoResult.getI2() + i2Type);
                alarmEventsReturn.setI3(equipmentInfoResult.getI3() + i3Type);
                alarmEventsReturn.setI4(equipmentInfoResult.getI4() + i4Type);
                alarmEventsReturn.setManagement(equipment.getManagement());

                returnList.add(alarmEventsReturn);
            }

        }

        if (returnList.size() == 0) {
            AlarmEventsReturn alarmEventsReturn = new AlarmEventsReturn(DateTimeUtils.getDateTime(), alarmEventsQuery.getEquipmentNumber(), equipment.getRegion(), equipment.getSite(), "无异常数据", "无异常数据", "无异常数据", "无异常数据", equipment.getManagement());
            returnList.add(alarmEventsReturn);
        }

        hashMap.put("items", returnList);
        hashMap.put("total", returnList.size());

        return Result.ok("true", 200, "成功", hashMap);
    }
}
